<?php


namespace Lib;


use Lib\Component\Factory;
use Lib\Component\Uri;

class Handle
{
    /**
     * 处理http请求
     * @return false|mixed|string
     * @throws \ReflectionException
     */
    public static function http()
    {
        // 获取工厂实例
        $factory = Factory::getFactory();
        // Uri 组件
        $uri = $factory->get(Uri::class);

        // 获取Uri参数
        $controller = $uri->segment(1);
        $function = $uri->segment(2);

        // 通过控制器名称和方法名实例化操作
        $action = new Acrion($controller, $function);

        // 执行操作获取结果
        $response = $action->run();

        // 数组转换成json
        if (is_array($response)) {
            return json_encode($response);
        } else {
            return $response;
        }
    }

    public static function handle404($error = '')
    {
        http_response_code(404);die;
    }

    public static function handle503($error = '')
    {
        var_dump($error);
        http_response_code(503);die;
    }
}